//
//  FoodMakeViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/27.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "FoodMakeViewController.h"
#import "HX_Macro.h"
#import "HXFoodBigModel.h"
#import "UIView+Frame.h"
#import "HXFoodSmallModel.h"
#import "HXNetTool.h"
#import "HXFoodCustomCell.h"
#define kTabelView @"tabel"
#import "HXFoodViewFlowLayout.h"
#import "HXFoodMadeVC.h"
#import "HXFoodMaterialVC.h"
@interface FoodMakeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) CGFloat heightCell;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, strong) HXFoodViewFlowLayout *layout;
@property (nonatomic, strong) NSString *image;
@property (nonatomic, strong) NSString *text;
/**
 *  里层数据
 */
@property (nonatomic, strong) NSMutableArray *array;
@end
@implementation FoodMakeViewController
- (NSMutableArray *)arrDataSource {
    if (!_arrDataSource) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}
- (NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.layout = [[HXFoodViewFlowLayout alloc] init];
    [self getNetWorkingWithModel];
    [self creatTabelView];
}
- (void)getNetWorkingWithModel {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=MaterialSubtype&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8", @"User-Agent" : @"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C)zhangchuAPPforAndroid4.4.0", @"Host" : @"api.izhangchu.com", @"Connection" : @"Keep-Alive"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *dicData = result;
        NSDictionary *dicAll = dicData[@"data"];
        NSArray *arrData = dicAll[@"data"];
        for (NSDictionary *dicSource in arrData) {
            HXFoodBigModel *bigModel = [HXFoodBigModel ModelWithDic:dicSource];
            [self.arrDataSource addObject:bigModel];
            [bigModel.data insertObject:bigModel.image atIndex:0];
            [self.array addObject:bigModel];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)creatTabelView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 110) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[HXFoodCustomCell class] forCellReuseIdentifier:kTabelView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arrDataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *lable = [[UILabel alloc] init];
    lable.HX_width = SCREEN_WIDTH;
    lable.HX_height = 40;
    lable.backgroundColor = [UIColor whiteColor];
    HXFoodBigModel *bigModel = self.arrDataSource[section];
    lable.font = [UIFont systemFontOfSize:15.0f];
    lable.textColor = [UIColor colorWithWhite:0.217 alpha:1.000];
    lable.text = bigModel.text;
    return lable;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXFoodBigModel *bigModel = self.arrDataSource[indexPath.section];
    NSInteger cellCount = (bigModel.data.count - 7);
    if (bigModel.data.count > 6) {
        if (cellCount % 5 == 0) {
            return cellCount / 5 * 40 + cellCount * 3 + 40 * 2;
        } else {
            return (cellCount / 5 + 3) * 40 + (cellCount + 5) * 2;
        }
    } else {
        return 40 * 2 + 2 * 3;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 50;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXFoodCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kTabelView];
    HXFoodBigModel *bigModel = self.arrDataSource[indexPath.section];
    cell.items = ^(NSInteger row) {
         HXFoodMaterialVC *material = [[HXFoodMaterialVC alloc] init];
        material.userId = [bigModel.data[row] ID];
        [self.navigationController pushViewController:material animated:YES];
    };
    HXFoodBigModel *model = self.array[indexPath.section];
    cell.arrDataSouce = model.data;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
